package yogiro.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import org.springframework.orm.ibatis.SqlMapClientTemplate;

import yogiro.to.MemberTO;
import yogiro.to.MessageTO;

public class MessageDAO {
private SqlMapClientTemplate sqlMap;
	
	public MessageDAO(SqlMapClientTemplate sqlMap){
		this.sqlMap = sqlMap;
	}
	
	//번호 자동 증가
	public int sequence() throws SQLException{
		return (int)sqlMap.queryForObject("messageSequence");
	}
	
	//글 삽입
	public Object insertMessage(MessageTO mto) throws SQLException{
		return sqlMap.insert("insertMessage",mto);
	}
	
	//메시지 삽입(client)
	public Object insertMessageClient(MessageTO mto) throws SQLException{
		return sqlMap.insert("insertMessageClient",mto);
	}
	
	//전체조회(admin)
	public ArrayList<MessageTO> selectAllMessage()throws SQLException{
		return (ArrayList<MessageTO>)sqlMap.queryForList("selectAllMessage");
	}
	
	// paging을 위한 sql문 page를 받아 한페이지당 몇개씩 list를 뿌려줄 껀지 정하는 메소드
	public ArrayList<MessageTO> selectBoardListByPage(int page,int contentPerPage)throws SQLException{
		HashMap map = new HashMap();
		map.put("rownum", contentPerPage);
		map.put("page", page);
		return (ArrayList<MessageTO>)sqlMap.queryForList("selectBoardListByPage",map);
	}
	
	//admin이 client에게 답장보낸것들
	public ArrayList<MessageTO> selectReplyBoardListByPage(int page,int contentPerPage)throws SQLException{
		HashMap map = new HashMap();
		map.put("rownum", contentPerPage);
		map.put("page", page);
		return (ArrayList<MessageTO>)sqlMap.queryForList("selectReplyBoardListByPage",map);
	}
	
	//client 받은쪽지함
	public ArrayList<MessageTO> selectReplyClientBoardByPage(int page,int contentPerPage,String messageReceiver)throws SQLException{
		HashMap map = new HashMap();
		map.put("rownum", contentPerPage);
		map.put("messageReceiver",messageReceiver);
		map.put("page", page);
		return (ArrayList<MessageTO>)sqlMap.queryForList("selectReplyClientBoardByPage",map);
	}
	
	//보낸쪽지함 (client)
	public ArrayList<MessageTO> selectClientBoardListByPage(int page,int contentPerPage,String messageWriter)throws SQLException{
		HashMap map = new HashMap();
		map.put("rownum", contentPerPage);
		map.put("messageWriter",messageWriter);
		map.put("page", page);
		return (ArrayList<MessageTO>)sqlMap.queryForList("selectClientBoardListByPage",map);
	}
	
	// 게시판에서 숫자를 기준으로 count를 하여 제일 마지막째줄 값을 리턴
	public int selectTotalContentCount()throws SQLException{
		return (int)sqlMap.queryForObject("selectTotalContentCount");
	}
	
	//admin 게시판 reply count
	public int selectReplyTotalContentCount()throws SQLException{
		return (int)sqlMap.queryForObject("selectReplyTotalContentCount");
	}
	
	//client 게시판 count(받은쪽지함)
	public int selectReplyClientTotalContentCount(String messageReceiver)throws SQLException{
		return (int)sqlMap.queryForObject("selectReplyClientTotalContentCount",messageReceiver);
	}
	
	//client 게시판 count(보낸쪽지함)
	public int selectClientTotalContentCount(String messageWriter)throws SQLException{
		return (int)sqlMap.queryForObject("selectClientTotalContentCount",messageWriter);
	}
	
	//전체쪽지중 id로 조회 count
	public int selectSearchTotalContentCount(String searchId)throws SQLException{
		return (int)sqlMap.queryForObject("selectSearchTotalContentCount",searchId);
	}
	
	//no값으로 message조회
	public MessageTO selectByNoMessage(int messageNo)throws SQLException{
		return (MessageTO)sqlMap.queryForObject("selectByNoMessage",messageNo);
	}
	
	//no값으로 delete
	public int deleteByNo(int messageNo)throws SQLException{
		return (int)sqlMap.delete("deleteByNo",messageNo);
	}
	
	//send=1 update(admin)
	public int updateSend(int messageNo)throws SQLException{
		return (int)sqlMap.update("updateSend",messageNo);
	}
	
	//receive=1 update(client)
	public int updateReceive(int messageNo)throws SQLException{
		return (int)sqlMap.update("updateReceive",messageNo);
	}
	
	//전체 받은 쪽지중 아이디로 검색
	public ArrayList<MessageTO> searchById(int contentPerPage, int page, String searchId)throws SQLException{
		HashMap map = new HashMap();
		map.put("rownum", contentPerPage);
		map.put("searchId",searchId);
		map.put("page", page);
		return (ArrayList<MessageTO>)sqlMap.queryForList("searchById",map);
	}
	
	//전체 보낸 쪽지중 아이디로 검색
	public ArrayList<MessageTO> replySearchById(int contentPerPage, int page, String searchId)throws SQLException{
		HashMap map = new HashMap();
		map.put("rownum", contentPerPage);
		map.put("searchId",searchId);
		map.put("page", page);
		return (ArrayList<MessageTO>)sqlMap.queryForList("replySearchById",map);
	}
	
	//MemberId 조회
	public ArrayList<MemberTO> selectMemberId()throws SQLException{
		return (ArrayList<MemberTO>)sqlMap.queryForList("selectMemberId");
	}
	
	//admin 아이디 조회
	public String selectAdmin()throws SQLException{
		return (String)sqlMap.queryForObject("selectAdmin");
	}
}
